<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable implements JWTSubject
{
    use Notifiable;
    //用户表
    protected $table = 'user';

    public static $modify = true;

    protected $fillable = [
        'id',
        'head_img',
        'phone',
        'wx_openid',
        'total_train_time',
        'expire_time',
        'total_integral',
        'user_number',
        'nickname'
    ];

    /**
     * Get the identifier that will be stored in the subject claim of the JWT.
     *
     * @return mixed
     */
    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    /**
     * Return a key value array, containing any custom claims to be added to the JWT.
     *
     * @return array
     */
    public function getJWTCustomClaims()
    {
        return [];
    }

    //头像 head_img
    public function getHeadImgAttribute($value){
        if(!empty($value)) {
            if(self::$modify)return getDomain() . '/' . $value;
            if(!self::$modify)return $value;
        }else{
            return null;
        }
    }

    public function getTotalIntegralAttribute($value){
        //dd($value,$this->expire_time,$this->id);
        $time = date("Y-m-d H:i:s");
        $expire_time = "{$this->expire_time} 23:59:59";

        $new_time = strtotime("{$time}");
        $expire_time = strtotime("{$expire_time}");

        if($expire_time < $new_time){
            return 0;
        }
        return $value;
    }

    public function getExpireTimeAttribute($value){
        // expire_time
        $expire_time = strtotime($value);

        if($expire_time < 0){
            return date("Y-m-d");
        }else{
            if(!empty($value)) {
                $value_time = explode(" ", $value);
                return $value_time[0];
            }else{
                return $value;
            }
        }
        return $value;
    }

    public function scopeWhereId($query, $user_id){
        return $query->where('id',$user_id);
    }

    //关联预约
    public function user_subscribe(){
        return $this->hasMany(UserSubscribe::class,'user_id','id');
    }

    //我的训练
    public function user_train(){
        return $this->hasMany(UserTrain::class,'user_id','id');
    }

    //关联等级
    public function user_level()
    {
        return $this->hasOne(UserRank::class,'id','user_rank_id');
    }

}
